/*
 * Class
 * ParameterSet
 */
package pl.abstractvoid.datamodel.parameters;

import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

/**
 * Represents the entire set of probability distribution's parameters.
 * <p>
 * This set is divided into two parts. One of them are SingularParameters and
 * the second part are ParameterTables. Both are described in particular docs.
 *
 * @see SingleParameter
 * @see ParameterTable
 *
 * @author Wojciech Szałapski
 * @author Ivo Zieliński
 */
public class ParameterSet implements Serializable {

    /**
     * The serial number for serialization and deserialization with different 
     * versions of the application.
     */
    static final long serialVersionUID = 1L;
    /**
     * Collection mapping String parameter's names into their SingleParameter
     * instances.
     * <p>
     * It is important to use the collection which preserves the order of 
     * elements no to change the hash and the appearance.
     */
    private TreeMap<String, SingleParameter> singleParameters;
    /**
     * Collection mapping String names of the parameter's tables into their
     * ParameterTable instances.
     * <p>
     * It is important to use the collection which preserves the order of 
     * elements no to change the hash and the appearance.
     */
    private TreeMap<String, ParameterTable> parameterTables;

    /**
     * Returns the hash of the whole parameter set.
     * 
     * @return Hash of the whole parameter set.
     */
    public double getParametersHash() {
        double hash = 0;
        if (parameterTables != null) {
            for (Map.Entry pairs : parameterTables.entrySet()) {
                ParameterTable tb = (ParameterTable) pairs.getValue();
                hash += tb.getHash();
            }
        }
        long multiplier = 2;
        if (singleParameters != null) {
            for (Map.Entry pairs : singleParameters.entrySet()) {
                SingleParameter sp = (SingleParameter) pairs.getValue();
                hash += sp.getHash() * multiplier;
                multiplier++;
            }
        }
        return hash;
    }

    /**
     * Creates an empty set of parameters with null references to both maps.
     */
    public ParameterSet() {
    }

    /**
     * Returns the collection of SingleParameters.
     *
     * @return TreeMap of parameters that occurs singly in the set of
     * distribution's parameters.
     */
    public TreeMap<String, SingleParameter> getSingleParameters() {
        return singleParameters;
    }

    /**
     * Return the collection of ParameterTables.
     *
     * @return TreeMap of parameters that occurs in the parameter set in the
     * form of tables.
     */
    public TreeMap<String, ParameterTable> getParameterTables() {
        return parameterTables;
    }

    /**
     * Sets the collection of SingleParameters for the distribution.
     *
     * @param singleParameters TreeMap of parameters that occurs singly in the
     * set of distribution's parameters.
     */
    public void setSingleParameters(TreeMap<String, SingleParameter> singleParameters) {
        this.singleParameters = singleParameters;
    }

    /**
     * Sets the collection of ParameterTables for the distribution.
     *
     * @param parameterTables TreeMap of parameters that occurs in the parameter
     * set in the form of tables.
     */
    public void setParameterTables(TreeMap<String, ParameterTable> parameterTables) {
        this.parameterTables = parameterTables;
    }
}
